package com.ruoyi.boat.service.impl;

import com.ruoyi.boat.aop.MyDataScope;
import com.ruoyi.boat.aop.SQLFilter;
import com.ruoyi.boat.domain.Statistic;
import com.ruoyi.boat.domain.Status;
import com.ruoyi.boat.mapper.StatusMapper;
import com.ruoyi.boat.service.IStatusService;
import com.ruoyi.boat.utils.DoubleUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 状态Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
public class StatusServiceImpl implements IStatusService {

    @Autowired
    ApplicationContext context;

    @Autowired
    private StatusMapper statusMapper;

    /**
     * 查询状态
     *
     * @param id 状态主键
     * @return 状态
     */
    @Override
    public Status selectStatusById(Long id) {
        return statusMapper.selectStatusById(id);
    }

    /**
     * 查询状态列表
     *
     * @param status 状态
     * @return 状态
     */
    @Override
    @MyDataScope
    @SQLFilter
    public List<Status> selectStatusList(Status status) {
        return statusMapper.selectStatusList(status);
    }

    /**
     * 查询平均偏差
     *
     * @param status 状态
     * @return 状态
     */
    @Override
    @MyDataScope
    @SQLFilter
    public Double selectAvgDeviation(Status status) {
        return statusMapper.selectAvgDeviation(status);
    }

    @Override
    public Map<String, Double> selectTodayThisWeekThisMonthAvgDeviation() {
        Status status = new Status();
        HashMap<String, Double> map = new HashMap<>();
        map.put("today", DoubleUtil.toFixed(context.getBean(this.getClass()).selectAvgDeviationOfToday(status), 2));
        map.put("week", DoubleUtil.toFixed(context.getBean(this.getClass()).selectAvgDeviationOfThisWeek(status), 2));
        map.put("month", DoubleUtil.toFixed(context.getBean(this.getClass()).selectAvgDeviationOfThisMonth(status), 2));
        return map;
    }

    public Double selectAvgDeviationOfToday(Status status) {
        String[] times = DateUtils.getBeginTimeAndEndTimeOfToday();
        status.setTime(null);
        status.getParams().put("beginTime", times[0]);
        status.getParams().put("endTime", times[1]);
        return context.getBean(this.getClass()).selectAvgDeviation(status);
    }

    public Double selectAvgDeviationOfThisWeek(Status status) {
        String[] times = DateUtils.getBeginTimeAndEndTimeOfThisWeek();
        status.setTime(null);
        status.getParams().put("beginTime", times[0]);
        status.getParams().put("endTime", times[1]);
        return context.getBean(this.getClass()).selectAvgDeviation(status);
    }

    public Double selectAvgDeviationOfThisMonth(Status status) {
        String[] times = DateUtils.getBeginTimeAndEndTimeOfThisMonth();
        status.setTime(null);
        status.getParams().put("beginTime", times[0]);
        status.getParams().put("endTime", times[1]);
        return context.getBean(this.getClass()).selectAvgDeviation(status);
    }

    @Override
    public List<Status> selectStatusListByBoatCode(String boatCode) {
        return statusMapper.selectStatusListByBoatCode(boatCode);
    }

    /**
     * 新增状态
     *
     * @param status 状态
     * @return 结果
     */
    @Override
    public int insertStatus(Status status) {
        status.setCreateTime(DateUtils.getNowDate());
        return statusMapper.insertStatus(status);
    }

    /**
     * 修改状态
     *
     * @param status 状态
     * @return 结果
     */
    @Override
    public int updateStatus(Status status) {
        return statusMapper.updateStatus(status);
    }

    /**
     * 批量删除状态
     *
     * @param ids 需要删除的状态主键
     * @return 结果
     */
    @Override
    public int deleteStatusByIds(String ids) {
        return statusMapper.deleteStatusByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除状态信息
     *
     * @param id 状态主键
     * @return 结果
     */
    @Override
    public int deleteStatusById(Long id) {
        return statusMapper.deleteStatusById(id);
    }

    @Override
    public Status selectLatestStatusByCode(String boatCode) {
        return statusMapper.selectLatestStatusByCode(boatCode);
    }
}
